package com.buildings.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.buildings.R;
import com.buildings.bean.LocalBean;

import java.util.List;

public class LocalLineAdapter extends RecyclerView.Adapter<LocalLineAdapter.ViewHolder> {

    private List<LocalBean> data;
    private Activity activity;
    private boolean isEdit = false;
    public LocalLineAdapter(Activity activity, List<LocalBean> data) {
        this.data = data;
        this.activity = activity;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_local_line_task, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.tvName.setText(data.get(position).getTitle());
        if (isEdit){
            holder.iv_select.setVisibility(View.VISIBLE);
        }else {
            holder.iv_select.setVisibility(View.GONE);
        }
        if (data.get(position).isCheck()){
            holder.iv_select.setImageResource(R.mipmap.icon_select_y);
        }else {
            holder.iv_select.setImageResource(R.mipmap.icon_select_n);
        }
        holder.iv_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener){
                    listener.onCheck(position);
                }
            }
        });
        //查看表单
        holder.ll_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener)
                listener.onItemClick(position);
            }
        });
        holder.tv_usb_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener)
                    listener.onDelete(position);
            }
        });
        holder.tv_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener)
                    listener.onEdit(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return data != null ? data.size() : 0;
    }

    public void setIsEdit(boolean b) {
        this.isEdit = b;
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvName;//查看表单
        Button tv_usb_delete;//查看表单
        RelativeLayout ll_item;//查看表单
        ImageView iv_select;//查看表单
        TextView tv_edit;//编辑表单

        public ViewHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
            tv_usb_delete = itemView.findViewById(R.id.tv_usb_delete);
            ll_item = itemView.findViewById(R.id.ll_item);
            iv_select = itemView.findViewById(R.id.iv_select);
            tv_edit = itemView.findViewById(R.id.tv_edit);
        }
    }

    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(int position);
        void onCheck(int position);
        void onDelete(int position);
        void onEdit(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

}
